package com.wande.dataplatform.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.wande.common.core.domain.R;
import com.wande.common.log.annotation.Log;
import com.wande.common.log.enums.BusinessType;
import com.wande.common.mybatis.core.page.PageQuery;
import com.wande.common.mybatis.core.page.TableDataInfo;
import com.wande.common.web.core.BaseController;
import com.wande.dataplatform.domain.dto.AssetCategoryDTO;
import com.wande.dataplatform.domain.dto.DataAssetDTO;
import com.wande.dataplatform.domain.vo.DataAssetStatisticsVO;
import com.wande.dataplatform.service.IAssetCategoryService;
import com.wande.dataplatform.service.IDataAssetService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 数据资产管理Controller
 *
 * @author ruoyi
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/dataplatform/asset")
public class DataAssetController extends BaseController {

    private final IDataAssetService assetService;
    private final IAssetCategoryService categoryService;

    /**
     * 查询数据资产列表
     */
    @SaCheckPermission("dataplatform:asset:list")
    @GetMapping("/list")
    public TableDataInfo<DataAssetDTO> list(DataAssetDTO dto, PageQuery pageQuery) {
        return assetService.queryPageList(dto, pageQuery);
    }

    /**
     * 获取数据资产详细信息
     */
    @SaCheckPermission("dataplatform:asset:query")
    @GetMapping("/{id}")
    public R<DataAssetDTO> getInfo(@PathVariable Long id) {
        return R.ok(assetService.queryById(id));
    }

    /**
     * 新增数据资产
     */
    @SaCheckPermission("dataplatform:asset:add")
    @Log(title = "数据资产", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Long> add(@RequestBody DataAssetDTO dto) {
        return R.ok(assetService.insertDataAsset(dto));
    }

    /**
     * 修改数据资产
     */
    @SaCheckPermission("dataplatform:asset:edit")
    @Log(title = "数据资产", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@RequestBody DataAssetDTO dto) {
        assetService.updateDataAsset(dto);
        return R.ok();
    }

    /**
     * 删除数据资产
     */
    @SaCheckPermission("dataplatform:asset:remove")
    @Log(title = "数据资产", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable List<Long> ids) {
        assetService.deleteByIds(ids);
        return R.ok();
    }

    /**
     * 更新资产状态
     */
    @SaCheckPermission("dataplatform:asset:edit")
    @Log(title = "数据资产", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}/status")
    public R<Void> updateStatus(@PathVariable Long id, @RequestParam String status) {
        assetService.updateStatus(id, status);
        return R.ok();
    }

    /**
     * 增加使用次数
     */
    @PostMapping("/{id}/use")
    public R<Void> incrementUseCount(@PathVariable Long id) {
        assetService.incrementUseCount(id);
        return R.ok();
    }

    /**
     * 搜索数据资产
     */
    @SaCheckPermission("dataplatform:asset:list")
    @GetMapping("/search")
    public TableDataInfo<DataAssetDTO> search(@RequestParam String keyword, PageQuery pageQuery) {
        return assetService.searchAssets(keyword, pageQuery);
    }

    /**
     * 获取资产统计信息
     */
    @SaCheckPermission("dataplatform:asset:list")
    @GetMapping("/statistics")
    public R<DataAssetStatisticsVO> getStatistics(@RequestParam(required = false) Long projectId) {
        return R.ok(assetService.getStatistics(projectId));
    }

    /**
     * 按分类统计资产数量
     */
    @SaCheckPermission("dataplatform:asset:list")
    @GetMapping("/count/category")
    public R<Map<String, Integer>> countByCategory(@RequestParam(required = false) Long projectId) {
        return R.ok(assetService.countByCategory(projectId));
    }

    /**
     * 按类型统计资产数量
     */
    @SaCheckPermission("dataplatform:asset:list")
    @GetMapping("/count/type")
    public R<Map<String, Integer>> countByType(@RequestParam(required = false) Long projectId) {
        return R.ok(assetService.countByType(projectId));
    }

    /**
     * 按分级统计资产数量
     */
    @SaCheckPermission("dataplatform:asset:list")
    @GetMapping("/count/level")
    public R<Map<String, Integer>> countByLevel(@RequestParam(required = false) Long projectId) {
        return R.ok(assetService.countByLevel(projectId));
    }

    // ==================== 资产分类管理 ====================

    /**
     * 查询资产分类树
     */
    @SaCheckPermission("dataplatform:asset:list")
    @GetMapping("/category/tree")
    public R<List<AssetCategoryDTO>> categoryTree(@RequestParam(required = false) Long projectId) {
        return R.ok(categoryService.queryTree(projectId));
    }

    /**
     * 查询资产分类列表
     */
    @SaCheckPermission("dataplatform:asset:list")
    @GetMapping("/category/list")
    public R<List<AssetCategoryDTO>> categoryList(AssetCategoryDTO dto) {
        return R.ok(categoryService.queryList(dto));
    }

    /**
     * 获取资产分类详细信息
     */
    @SaCheckPermission("dataplatform:asset:query")
    @GetMapping("/category/{id}")
    public R<AssetCategoryDTO> getCategoryInfo(@PathVariable Long id) {
        return R.ok(categoryService.queryById(id));
    }

    /**
     * 新增资产分类
     */
    @SaCheckPermission("dataplatform:asset:add")
    @Log(title = "资产分类", businessType = BusinessType.INSERT)
    @PostMapping("/category")
    public R<Long> addCategory(@RequestBody AssetCategoryDTO dto) {
        return R.ok(categoryService.insertCategory(dto));
    }

    /**
     * 修改资产分类
     */
    @SaCheckPermission("dataplatform:asset:edit")
    @Log(title = "资产分类", businessType = BusinessType.UPDATE)
    @PutMapping("/category")
    public R<Void> editCategory(@RequestBody AssetCategoryDTO dto) {
        categoryService.updateCategory(dto);
        return R.ok();
    }

    /**
     * 删除资产分类
     */
    @SaCheckPermission("dataplatform:asset:remove")
    @Log(title = "资产分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/category/{ids}")
    public R<Void> removeCategory(@PathVariable List<Long> ids) {
        categoryService.deleteByIds(ids);
        return R.ok();
    }

}
